﻿#region Using Directives

using System;
using System.IO;
using System.Windows.Forms;
using System.Globalization;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using Microsoft.Practices.RecipeFramework.Services;
using Microsoft.Practices.RecipeFramework.VisualStudio;
using Microsoft.Practices.RecipeFramework.VisualStudio.Templates;
using EnvDTE;

#endregion

namespace SteriaMummert.SharePoint.SoftwareFactoryLite.Actions
{
    /// <summary>
    /// </summary>
    [ServiceDependency(typeof(DTE))]
    public class CustomTextTemplateAction : Microsoft.Practices.RecipeFramework.VisualStudio.Library.Templates.T4Action// Microsoft.Practices.RecipeFramework.VisualStudio.Library.DynamicInputAction
    {
        public CustomTextTemplateAction()
        {
            
        }

        [Output]
        public string Content
        {
            get
            {
                return this.rendered;
            }
            set
            {
                this.rendered = value;
            }
        }
        private string rendered = "";

        [Input(true)]
        public string Template
        {
            get
            {
                return this.template;
            }
            set
            {
                this.template = value;
            }
        }
        private string template = "";

        public override void Execute()
        {
            EnvDTE.DTE vs = this.GetService<EnvDTE.DTE>(true);
            Project a = vs.Solution.Projects.Item(1); //.FullName.ToString();
            string projectpath = a.Properties.Item("FullPath").Value.ToString();
            string solutionpath = projectpath.Substring(0,projectpath.LastIndexOf('\\', projectpath.Length - 2));
            string solutionname = solutionpath.Substring(solutionpath.LastIndexOf('\\')+1);

            this.additionalArguments.Add("SolutionName", solutionname + ".sln");
            string template = this.Template;

            string templateCode = string.Empty;
            if (template == null)
            {
                throw new ArgumentNullException("Template");
            }
            string templateBasePath = base.GetTemplateBasePath();
            if (!Path.IsPathRooted(template))
            {
                template = Path.Combine(templateBasePath, template);
            }
            template = new FileInfo(template).FullName;
            if (!template.StartsWith(templateBasePath))
            {
                throw new ArgumentException("Starts not with " + templateBasePath);
            }
            templateCode = File.ReadAllText(template);
            this.Content = base.Render(templateCode, template);
        }

        public override void Undo()
        {
            
        }

 

    }
}